package org.oschina.api.user.bar.dao;

import java.util.ArrayList;
import java.util.List;
import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.user.bar.vo.UserBarRole;
import org.oschina.api.user.role.vo.UserBarRoleVo;
import org.oschina.api.user.role.vo.UserBarVo;
import org.springframework.stereotype.Repository;

@Repository
public class UserBarRoleDAO extends BaseDAO {

	/**
	 * 更新
	 * @param role
	 * @param barRoles
	 * @return
	 */
	public int[] update(int role,List<UserBarRole> barRoles)
	{
		getQueryHelper().delete_cache(new String[]{"role=?"}, new Object[]{role}, UserBarVo.class);
		
		List<Object[]> args=new ArrayList<Object[]>();
		for(UserBarRole barRole:barRoles)
			args.add(new Object[]{barRole.getId(),barRole.getBar(),barRole.getRole()});
		return getQueryHelper().batchUpdate_cache(Constants.CACHE_USER_BAR_ROLE, "INSERT "+Constants.DB_USER_BAR_ROLE+"(id,bar,role) VALUES(?,?,?)", args);
	}
	
	/**
	 * 根据bar删除
	 * @param bar
	 * @return
	 */
	public int removeByBar(int bar)
	{
		return (int) getQueryHelper().delete_cache(new String[]{"bar=?"}, new Object[]{bar},UserBarRoleVo.class);
	}
	
	/**
	 * 根据角色删除
	 * @param role
	 * @return
	 */
	public int removeByRole(int role)
	{
		return (int) getQueryHelper().delete_cache(new String[]{"role=?"}, new Object[]{role},UserBarRoleVo.class);
	}
}